/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2jserver.gameserver.model.actor.instance;

import java.util.StringTokenizer;

import com.l2jserver.Config;
import com.l2jserver.gameserver.ThreadPoolManager;
import com.l2jserver.gameserver.faction.Faction;
import com.l2jserver.gameserver.faction.FactionManager;
import com.l2jserver.gameserver.network.clientpackets.Say2;
import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
import com.l2jserver.gameserver.network.serverpackets.SocialAction;
import com.l2jserver.gameserver.network.serverpackets.TutorialCloseHtml;
import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;

/**
 * @author Laikeriz
 */
public class L2FactionManagerInstance extends L2NpcInstance
{
	/**
	 * @param template
	 */
	public L2FactionManagerInstance(int objectId, L2NpcTemplate template)
	{
		super(objectId, template);
	}
	
	@Override
	public void onSpawn()
	{
		super.onSpawn();
	}
	
	@Override
	public String getHtmlPath(int npcId, int val)
	{
		return "data/html/faction/npc" + (val == 0 ? "" : "-" + val) + ".htm";
	}
	
	@Override
	public void onBypassFeedback(L2PcInstance player, String command)
	{
		StringTokenizer st = new StringTokenizer(command, " ");
		String actualCommand = st.nextToken(); // Get actual command
		
		if (actualCommand.startsWith("selectFaction"))
		{
			if (st.countTokens() < 1 || !FactionManager.isQualifiedToSelectFaction(player))
				return;
			int val = Integer.parseInt(st.nextToken());
			broadcastPacket(new CreatureSay(getObjectId(), Say2.ALL, getName(), "Come on " + player.getName() + ", you have joined " + FactionManager.getFactionById(val).getName() + " faction, run to fight!"));
			player.broadcastPacket(new SocialAction(player.getObjectId(), 3));
			MagicSkillUse MSU = new MagicSkillUse(player, player, 5966, 1, 1, 0);
			player.sendPacket(MSU);
			player.broadcastPacket(MSU);
			player.setFaction(FactionManager.getFactionById(val));
			ThreadPoolManager.getInstance().scheduleGeneral(new SetFaction(val, player), 2500);
			if (Config.TUTORIAL_FACTION_CHOOSE)
				player.sendPacket(new TutorialCloseHtml());
		}
		else
			super.onBypassFeedback(player, command);
	}
	
	@Override
	public void showChatWindow(L2PcInstance player, int val)
	{
		NpcHtmlMessage msg = new NpcHtmlMessage(0);
		msg.setFile(getHtmlPath(getNpcId(), val));
		StringBuilder replace = new StringBuilder();
		for (Faction faction : FactionManager.factions.values())
			replace.append("<a action=\"bypass -h npc_%objectId%_selectFaction " + faction.getFactionId() + "\" msg=\"1987;Are you sure you want to join " + faction.getName() + " faction?\"><font color=" + faction.stringNameColor + ">" + faction.getName() + "</font></a>");
		msg.replace("%buttons%", replace.toString());
		msg.replace("%objectId%", String.valueOf(getObjectId()));
		player.sendPacket(msg);
	}
	
	public static class SetFaction implements Runnable
	{
		int id = 0;
		L2PcInstance player;
		
		public SetFaction(int _id, L2PcInstance _player)
		{
			id = _id;
			player = _player;
		}
		
		@Override
		public void run()
		{
			Faction faction = FactionManager.getFactionById(id);
			faction.setPlayerInThis(player, true);
		}
	}
}
